package org.andao.commons.taglib.ui;

import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.andao.commons.cache.CacheAppManager;
import org.andao.commons.model.Dictionary;
import org.andao.commons.taglib.BaseTagSupport;
import org.andao.core.config.Constants;
import org.andao.core.utils.ExStringUtils;
import org.springframework.web.servlet.support.RequestContext;



/**
 * 字典select标签.<p>
 * 
 * @author Marco.hu(hzg139@163.com)
 */
public class CustomSelectTag extends BaseTagSupport {

	private static final long serialVersionUID = 5140979292750090983L;

	private String id;

	private String name;

	private String classCss; // 样式相当html中class

	private String style;

	private String disabled;

	private String onblur;

	private String onchange;

	private String onclick;

	private String ondblclick;

	private String onfocus;
	
	private String dictionaryCode; // 字典编码

	private String value;
	
	private String validate; //验证类型
	
	private String mes; //验证信息
	
	private String choose="Y";
	
	private String size="10";
	
	private String multiple;//多选列表
	
	private String filtrationStr;  //保留值 以逗号","分隔

	private String isSubOptionText;//是否需要截去一部分长度显示option的值
	
	private String datatype;
	
	private String nullmsg;
	
	private String sucmsg;
	
	private String type; //选择框的内容类型
	
	@Override
	public int doEndTag() throws JspException {
		RequestContext ctx = new RequestContext(getrequest());
		StringBuilder sb = new StringBuilder("<select");
		if(name!=null && !name.equals("")){
			if (ExStringUtils.isEmpty(id)) 
				sb.append(" id='" + name + "'");
			else 
				sb.append(" id='" + id + "'");
			sb.append(" name='"+name+"'");
		}
		if(classCss!=null && !classCss.equals("")){
			sb.append(" class='"+classCss+"'");
		}
		if(style!=null && !style.equals("")){
			sb.append(" style='"+style+"'");
		}
		if(disabled!=null && !disabled.equals("") && (disabled.equals("true") || disabled.equals("disabled"))){
			sb.append(" disabled='"+disabled+"'");
		}
		if(onblur!=null && !onblur.equals("")){
			sb.append(" onblur='"+onblur+"'");
		}
		if(onchange!=null && !onchange.equals("")){
			sb.append(" onchange='"+onchange+"'");
		}
		if(onclick!=null && !onclick.equals("")){
			sb.append(" onclick='"+onclick+"'");
		}
		if(ondblclick!=null && !ondblclick.equals("")){
			sb.append(" ondblclick='"+ondblclick+"'");
		}
		if(onfocus!=null && !onfocus.equals("")){
			sb.append(" onfocus='"+onfocus+"'");
		}
		if (validate != null && !validate.equals("")) {
			sb.append(" validate='" + validate + "'");
		}
		if (mes != null && !mes.equals("")) {
			sb.append(" mes='" + mes + "'");
		}
//		if (ExStringUtils.isNotEmpty(size)) {
//			sb.append(" size='" + size + "'");
//		}
		if (multiple != null && !multiple.equals("")) {
			sb.append(" multiple='" + multiple + "'");
		}
		if(datatype != null && !datatype.equals("")){
			sb.append("datatype='" + datatype + "'");
		}
		if(nullmsg != null && !nullmsg.equals("")){
			sb.append("nullmsg='" + nullmsg + "'");
		}
		if(sucmsg != null && !sucmsg.equals("")){
			sb.append("sucmsg='" + sucmsg + "'");
		}
		sb.append(" >");
		String select_info=ctx.getMessage("SELECT");
		String defaultOption = (ExStringUtils.isBlank(select_info) ? "请选择":select_info);
		if(type != null){
			defaultOption += " "+type;
		}
		if(choose.equals("Y"))sb.append("<option value=''>"+defaultOption+"</option>");
		
		if(dictionaryCode!=null && !dictionaryCode.equals("")){
			//从缓存中获取字典对象
			List<Dictionary> children  = CacheAppManager.getChildren(dictionaryCode);
			if(children != null){
				for(Dictionary childDict : children){
					//保留值
					if(ExStringUtils.isNotBlank(filtrationStr) && filtrationStr.indexOf(childDict.getDictValue())<0){							
						continue;							
					}
					if(Constants.BOOLEAN_YES.equals(childDict.getIsUsed())){ //启用						
						sb.append("<option value='"+ExStringUtils.stripToEmpty(childDict.getDictValue())+"'");
						if(value!=null && !value.equals("")){ //初始化默认值
							if(value.equals(childDict.getDictValue()))sb.append(" selected='selected'");
						}else if(ExStringUtils.isNotEmpty(childDict.getIsDefault()) && Constants.BOOLEAN_YES.equals(childDict.getIsDefault())){
							sb.append(" selected='selected'");						
						}
						sb.append(">");
						//sb.append(childDict.getDictName());
						if("Y".equals(isSubOptionText)&&childDict.getDictName().length()>10) {
							sb.append(childDict.getDictName().substring(0, 9)+"...");
						}else{
							sb.append(childDict.getDictName());
						}	
						sb.append("</option>");
					}
				}
			}			
		}
		
		
		sb.append("</select>");
		JspWriter writer = this.pageContext.getOut();
		try {
			writer.append(sb.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getClassCss() {
		return classCss;
	}

	public void setClassCss(String classCss) {
		this.classCss = classCss;
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	public String getDisabled() {
		return disabled;
	}

	public void setDisabled(String disabled) {
		this.disabled = disabled;
	}

	public String getOnblur() {
		return onblur;
	}

	public void setOnblur(String onblur) {
		this.onblur = onblur;
	}

	public String getOnchange() {
		return onchange;
	}

	public void setOnchange(String onchange) {
		this.onchange = onchange;
	}

	public String getOnclick() {
		return onclick;
	}

	public void setOnclick(String onclick) {
		this.onclick = onclick;
	}

	public String getOndblclick() {
		return ondblclick;
	}

	public void setOndblclick(String ondblclick) {
		this.ondblclick = ondblclick;
	}

	public String getOnfocus() {
		return onfocus;
	}

	public void setOnfocus(String onfocus) {
		this.onfocus = onfocus;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getDictionaryCode() {
		return dictionaryCode;
	}

	public void setDictionaryCode(String dictionaryCode) {
		this.dictionaryCode = dictionaryCode;
	}

	public String getValidate() {
		return validate;
	}

	public void setValidate(String validate) {
		this.validate = validate;
	}

	public String getMes() {
		return mes;
	}

	public void setMes(String mes) {
		this.mes = mes;
	}

	public String getChoose() {
		return choose;
	}

	public void setChoose(String choose) {
		this.choose = choose;
	}

	public String getSize() {
		return size;
	}

	public void setSize(String size) {
		this.size = size;
	}
	
	public String getMultiple() {
		return multiple;
	}

	public void setMultiple(String multiple) {
		this.multiple = multiple;
	}
	
	public String getIsSubOptionText() {
		return isSubOptionText;
	}

	public void setIsSubOptionText(String isSubOptionText) {
		this.isSubOptionText = isSubOptionText;
	}
	public String getFiltrationStr() {
		return filtrationStr;
	}

	public void setFiltrationStr(String filtrationStr) {
		this.filtrationStr = filtrationStr;
	}

	public String getDatatype() {
		return datatype;
	}

	public void setDatatype(String datatype) {
		this.datatype = datatype;
	}

	public String getNullmsg() {
		return nullmsg;
	}

	public void setNullmsg(String nullmsg) {
		this.nullmsg = nullmsg;
	}

	public String getSucmsg() {
		return sucmsg;
	}

	public void setSucmsg(String sucmsg) {
		this.sucmsg = sucmsg;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
	
}
